package event.collide;

import processing.core.PApplet;
import processing.core.PVector;
import run.Doodler;
import run.Env;
import run.LineCell;

public class RandomPastPerpendicularSpawnRule implements CollideRule {

	public void executeRule(LineCell c, LineCell collided) {
		
		// so it doesn't get super insanely dense
		if (c.getAge() < 5) {
			return;
		}
		
		PApplet p = Doodler.instance;
		Env e = Env.instance;
		
		// cap the spawn rate
		int num = (int) p.random(10 - c.generation);
		
		for (int i =0; i < num; i++) {
			PVector dir = c.dir;
			PVector prevPos;
			int size = c.pastLocs.size();
			if (size > 0) {
				prevPos = c.pastLocs.get((int)p.random(size - 1));
				
				float choose = p.random(0,1);
				float z = 1;
				if (choose < 0.5)
					z = -1;
				
		    	PVector zh = new PVector(0,0,z);
		    	PVector perpendic = zh.cross(dir);
		    	
		    	perpendic.mult(2F);
		    	prevPos.add(perpendic);
		    	//TODO problem is we just hit back on ourselves causing that stupid weird dot
		    	perpendic.normalize();
		    	
		    	LineCell child = new LineCell(prevPos, perpendic, c.generation + 1);
		    	if (e.isCollide(child) == null)
		    		e.addCell(child);
			}
		}
	}

}
